<template>
  <transition name="modal">
      <div class="modal-mask" @click="$emit('close')">
          <div class="modal-container" @click="preventClick($event)">

              <div class="modal-header">
                <slot name="header">
                  <h3>动态密码登录</h3>
                  <img src="../assets/close.png" class="close" @click="$emit('close')">
                </slot>
              </div>

              <div class="modal-body">
                <slot name="body">
                  <div class="sq-input-group">
                    <input type="number" id="sq-input-mobile" name="phone" @keypress='validatePhone($event)' v-model="phone" placeholder="请输入手机号码" maxlength="11">
                  </div>
                  <div class="sq-input-group">
                    <input type="number" id="sq-input-code" maxlength="6" @keypress='validateCode($event)' @keyup.enter="login" name="code" v-model="code" placeholder="请输入动态密码">
                    <button :disabled="leftSeconds > 0" @click="sendCode" style="outline: none">{{sendCodeButtonText}}</button>
                  </div>
                  <div class="sq-input-group" style="flex-direction: column;">
                    <div class="errtips">{{errtips}}</div>
                    <div class="usertips">本人已阅读并同意<a href="http://m.sstt.iflyvoice.com/user_agreement.html" target="_blank">用户协议</a></div>
                  </div>
                </slot>
              </div>

              <div class="modal-footer">
                <slot name="footer">
                  <button class="modal-default-button" @click="checkLogin" style="outline: none">
                    登录
                  </button>
                </slot>
              </div>

          </div>
      </div>
  </transition>
</template>


<script>
//import Vue from 'vue'
import { mapGetters } from 'vuex';
import { login } from '../api/index'

const RESEND_CODE_INTERVAL = 30;

export default {
  name: 'sq-login-box',
  data () {
    return {
      phone: "",
      code: "",
      errtips: "",
      leftSeconds: 0
    }
  },
  created: function() {
    this.countDown();
  },
  methods: {
    checkLogin() {
      //check param
      if(!this.phone){
        this.errtips = "请输入手机号码"
        return false;
      }
      var reg = /^1[0-9]{10}$/;
      if(!reg.test(this.phone)){
        this.errtips = "请输入正确的手机号码"
        return false;
      }
      if(!this.code){
        this.errtips = "请输入验证码"
        return false;
      }
      reg = /^[0-9]{6}$/;
      if(!reg.test(this.code)){
        this.errtips = "请输入正确的验证码"
        return false;
      }
      var that = this;
      login(this.phone, this.code, this.channel).then(function(resp){
        if(resp.code == 0){
          window._hmt.push(['_trackEvent', '登录', '登录成功']);
          that.$store.dispatch('updateLastLoginPhone', {
            phone: that.phone
          });
          that.$store.dispatch('loginSuccess');
          that.$emit('close');
        }else{
          window._hmt.push(['_trackEvent', '登录', '登录失败', resp.msg]);
          that.errtips = resp.msg;
        }
      }).catch(function(err){
          console.log(err);
      });
    },
    _getResendLeftSeconds() {
      var lastSendCodeTime = parseInt(this.$store.getters.lastSendCodeTime);
      var now = (new Date()).getTime();
      var left =  Math.floor((now - lastSendCodeTime)/1000);
      if(left < 0){
          return RESEND_CODE_INTERVAL;
      } else if (left > RESEND_CODE_INTERVAL){
          return 0;
      }else{
          return RESEND_CODE_INTERVAL - left;
      }
    },
    countDown() {
      this.leftSeconds = this._getResendLeftSeconds();
      if(this.leftSeconds > 0){
        setTimeout(this.countDown, 1000);
      }
    },
    sendCode() {
      if(!this.phone){
        this.errtips = "请输入手机号码"
        return false;
      }
      var reg = /^1[0-9]{10}$/;
      if(!reg.test(this.phone)){
        this.errtips = "请输入正确的手机号码"
        return false;
      }
      if(this.leftSeconds > 0) {
        return false;
      }else{
        this.$store.dispatch("sendCode", {phone: this.phone});
        this.countDown();
      }
    },
    validatePhone(event) {
      if(event.keyCode == 46 || event.keyCode == 8) {
        return true;
      }
      var isNumber = event.charCode >= 48 && event.charCode <= 57
      var res = isNumber && this.phone.length < 11;
      if(!res) {
          event.preventDefault();
      }
      return res;
    },
    validateCode(event) {
      if(event.keyCode == 46 || event.keyCode == 8 || event.key == 13) {
        return true;
      }
      var isNumber = event.charCode >= 48 && event.charCode <= 57
      var res = isNumber && this.code.length < 6;
      if(!res) {
        event.preventDefault();
      }
      return res;
    },
    preventClick(event) {
      if(event.target.tagName === 'A' || event.target.tagName === 'a' || event.target.nodeName === 'A' || event.target.nodeName === 'a') {
        return true;
      }
      event.preventDefault();
      event.stopPropagation();
    }
  },
  computed: {
    sendCodeButtonText: function(){
      if(this.leftSeconds > 0) {
        return this.leftSeconds + "秒后重试";
      }else{
        return "获取动态密码";
      }
    },
    ...mapGetters([
      'channel'
    ])
  }
}
</script>

<style>
textarea:focus, input:focus {
    outline: none;
}
</style>

<style scoped>

.modal-mask {
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  height: 100vh;
  background-color: rgba(0, 0, 0, .5);
  transition: opacity .3s ease;
  display: flex;
  align-content: center;
  justify-content: center;
}

.modal-container {
  width: 100vw;
  width: 100%;
  max-width: 400px;
  margin: auto 12px;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
  transition: all .3s ease;
  font-family: Helvetica, Arial, sans-serif;
  height: 300px;
  position: relative;
}

.modal-header h3 {
  margin-top: 0;
  color: #000;
  height: 22px;
  line-height: 22px;
  font-size: 18px;
  margin-top: 18px;
  margin-bottom: 18px;
  font-weight: normal;
}

.modal-header .close {
  position: absolute;
  top: 0px;
  right: 0px;
  height: 16px;
  width: 16px;
  padding: 12px;
}

.modal-body {
  margin: 20px 0 12px 0;
}
.sq-input-group {
  display: flex;
  align-items: center;
  padding-left: 28px;
  padding-right: 28px;
}

.sq-input-group label {
  flex-basis: 70px;
  flex-shrink: 0;
}

.sq-input-group input {
  flex-grow: 1;
  height: 48px;
  border: 1px solid #ccc;
  padding-left: 12px;
  flex: 1;
  font-size: 14px;
}

#sq-input-mobile {
  margin-bottom: 12px;
}

#sq-input-code {
  width: 60%;
  flex-grow: 1;
  min-width: 120px;
  max-width: 180px;
}

.sq-input-group button {
  width: 50%;
  min-width: 100px;
  background-color: #fff;
  font-size: 14px;
  color:#3385ff;
  padding: 0px;
  border: 0px;
  outline: none;
  flex-grow: 1;
  flex-basis: 100px;
}

.errtips {
  margin-top: 10px;
  font-size: 14px;
  height: 24px;
  padding-left: 12px;
  color: #ff4a3d;
  align-self: flex-start;
}

.usertips {
  margin-top: 0;
  font-size: 14px;
  height: 14px;
  color: #999999;
  align-self: flex-end;
}

.usertips a {
  text-decoration: none;
  color:#3385ff;
}
.modal-footer {
  height: 44px;
  display: flex;
}

.modal-footer button{
  height: 100%;
  border: 0px;
  flex-grow: 1;
  margin-left: 28px;
  margin-right: 28px;
  color: #fff;
  background-color: #fe6b40;
  font-size: 16px;
}

.modal-default-button {
  float: right;
}

.modal-enter {
  opacity: 0;
}

.modal-leave-active {
  opacity: 0;
}

.modal-enter .modal-container,
.modal-leave-active .modal-container {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

</style>
